package com.ihr360.payroll.service.payroll.mock.core;

import com.ihr360.context.SpringContext;
import com.ihr360.payroll.calc.StaffSalaryCalcPeriod;
import com.ihr360.payroll.dao.PayrollConfigDemoRepository;
import com.ihr360.payroll.model.dto.salary.demo.PayrollConfigDemo;
import com.ihr360.payroll.model.dto.salary.meta.SalaryField;
import com.ihr360.payroll.service.salarytasklet.step.calculator.PersonalSalaryCalculator;
import com.ihr360.validation.ValidationUtils;

import java.util.List;

public class MockPersonalSalaryCalcContextHolder {

    private static final ThreadLocal<PersonalSalaryCalcContext> contextThreadLocal = new ThreadLocal<>();


    public static PersonalSalaryCalculator getCurrentCalculator() {
        PersonalSalaryCalcContext context = getMockPersonalSalaryCalcContext();
        PersonalSalaryCalculator calculator = context.getCalculator();
        if (calculator == null) {
            calculator = new PersonalSalaryCalculator();
            context.setCalculator(calculator);
        }
        return calculator;
    }

    public static StaffSalaryCalcPeriod getCurrentCalcdResult() {
        PersonalSalaryCalcContext context = getMockPersonalSalaryCalcContext();
        StaffSalaryCalcPeriod calcdResult = context.getCalcPeriod();
        if (calcdResult == null) {
            calcdResult = new StaffSalaryCalcPeriod();
            context.setCalcPeriod(calcdResult);
        }
        return calcdResult;
    }

    public static String getStaffId() {
        PersonalSalaryCalcContext context = getMockPersonalSalaryCalcContext();
        return context.getStaffId();
    }

    public static String getStaffName() {
        PersonalSalaryCalcContext context = getMockPersonalSalaryCalcContext();
        return context.getStaffName();
    }

    public static List<SalaryField> getSalaryField() {
        PersonalSalaryCalcContext context = getMockPersonalSalaryCalcContext();
        return context.getSalaryFields();
    }


    public static void setCalcdResult(StaffSalaryCalcPeriod calcdResult) {
        PersonalSalaryCalcContext context = getMockPersonalSalaryCalcContext();
        if (calcdResult == null) {
            calcdResult = new StaffSalaryCalcPeriod();
        }
        context.setCalcPeriod(calcdResult);
    }

    public static PersonalSalaryCalcContext getDemoPersonalSalaryCalcContext() {
        PersonalSalaryCalcContext context = contextThreadLocal.get();
        if (context == null) {
            PayrollConfigDemoRepository payrollConfigDemoRepository = SpringContext.getBean(PayrollConfigDemoRepository.class);
            PayrollConfigDemo configDemo = payrollConfigDemoRepository.findByEffectiveIs(true);
            ValidationUtils.rejectIfEmpty(configDemo, null, "薪资示例数据不存在！");
            context = new PersonalSalaryCalcContext();
            context.setStaffId(configDemo.getName());
            context.setStaffId("1");
            contextThreadLocal.set(context);
        }
        return context;
    }

    public static PersonalSalaryCalcContext getMockPersonalSalaryCalcContext() {
        PersonalSalaryCalcContext context = contextThreadLocal.get();
        if (context == null) {
            context = new PersonalSalaryCalcContext();
            context.setCalcPeriod(new StaffSalaryCalcPeriod());
            context.setCalculator(new PersonalSalaryCalculator());
            contextThreadLocal.set(context);
        }
        return context;
    }

}
